/**
 * @project_name staff_vehicle
 * @package_name com.ssh.dao
 * @desc TODO
 * @param
 * @author jack
 * @date 2013-4-23
 * @time 下午1:35:59
 */
package com.ssh.dao;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.ssh.entity.VisitorTable;
import com.ssh.model.pdata.VVisitInfoPData;

/**
 * @author jack
 *
 */
public class VisitorTableDAOImpl implements VisitorTableDAOI {
	private SessionFactory sessionFactory;

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.VisitorTableDAOI#addVTable(com.ssh.entity.VisitorTable)
	 */
	@Override
	public VisitorTable addVTable(VisitorTable visitorTable) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().save(visitorTable);
		return visitorTable;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.ssh.dao.VisitorTableDAOI#addVTable(com.ssh.entity.VisitorTable[])
	 */
	@Override
	public int addVTable(VisitorTable... visitorTable) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.VisitorTableDAOI#deleteVTable(java.lang.String[])
	 */
	@Override
	public int deleteVTable(String... strings) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.VisitorTableDAOI#deleteVTableById(java.lang.String)
	 */
	@Override
	public VisitorTable deleteVTableById(String visitorId) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.VisitorTableDAOI#getAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<VisitorTable> getAll() {
		// TODO Auto-generated method stub
		String hql = "from VisitorTable as v";
		return sessionFactory.getCurrentSession().createQuery(hql).list();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.VisitorTableDAOI#getAllVVisitorInfo()
	 *//**按离开时间查询来访人员*/
	@SuppressWarnings("unchecked")
	@Override
	public List<VVisitInfoPData> getAllVVisitorInfo() {
		// TODO Auto-generated method stub
		String hql = "select new com.ssh.model.pdata.VVisitInfoPData(vi,v) "
				+ "from VisitorTable as v,Visitor as vi where v.visitorId=vi.visitorId";
		return sessionFactory.getCurrentSession().createQuery(hql).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VVisitInfoPData> getVVisitorByInTime(Timestamp inTime) {
		// TODO Auto-generated method stub
		long endTime = inTime.getTime()+15*60*1000;
		long startTime = inTime.getTime()-15*60*1000;
		Timestamp endTimes=new Timestamp(0),startTimes = new Timestamp(0);
		startTimes.setTime(startTime);
		endTimes.setTime(endTime);
		String hql = "select new com.ssh.model.pdata.VVisitInfoPData(vi,v) "
				+ "from VisitorTable as v,Visitor as vi where v.visitorId=vi.visitorId" +
				" and v.VInTime between :startTimes and :endTimes ";
		Query query = sessionFactory.getCurrentSession().createQuery(hql).
				setParameter("startTimes", startTimes).setParameter("endTimes", endTimes);
		return query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<VVisitInfoPData> getVVisitorByOffTime(Timestamp offTime) {
		// TODO Auto-generated method stub
		long endTime = offTime.getTime()+30*60*1000;
		long startTime = offTime.getTime()-30*60*1000;
		Timestamp endTimes=new Timestamp(0),startTimes = new Timestamp(0);
		startTimes.setTime(startTime);
		endTimes.setTime(endTime);
		String hql = "select new com.ssh.model.pdata.VVisitInfoPData(vi,v) "
				+ "from VisitorTable as v,Visitor as vi where v.visitorId=vi.visitorId" +
				" and v.VOffTime between :startTimes and :endTimes ";
		Query query = sessionFactory.getCurrentSession().createQuery(hql).
				setParameter("startTimes", startTimes).setParameter("endTimes", endTimes);
		return query.list();
	}

	/**
	 * @return the sessionFactory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.VisitorTableDAOI#queryVTable(java.lang.String[])
	 */
	@Override
	public List<VisitorTable> queryVTable(String... strings) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public VisitorTable queryVTableById(String visitorId) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.VisitorTableDAOI#queryVTableById(java.lang.String)
	 */
	@Override
	public VisitorTable queryVTableById(String visitorId, String state) {
		// TODO Auto-generated method stub
		try {
			String hql = "from VisitorTable as v where v.visitorId=:visitorId and v.state=:state";
			Query query = sessionFactory.getCurrentSession().createQuery(hql)
					.setParameter("visitorId", visitorId)
					.setParameter("state", state);
			return (VisitorTable) query.uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * @param sessionFactory
	 *            the sessionFactory to set
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.ssh.dao.VisitorTableDAOI#updateVTable(com.ssh.entity.VisitorTable)
	 */
	@Override
	public VisitorTable updateVTable(VisitorTable visitorTable) {
		// TODO Auto-generated method stub
		try{
			sessionFactory.getCurrentSession().update(visitorTable);
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return visitorTable;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.ssh.dao.VisitorTableDAOI#updateVTable(com.ssh.entity.VisitorTable[])
	 */
	@Override
	public int updateVTable(VisitorTable... visitorTable) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public VVisitInfoPData getVByIdState(String visitorId, String state) {
		// TODO Auto-generated method stub
		try {
			String hql = "select new com.ssh.model.pdata.VVisitInfoPData(v,vt) " +
					" from VisitorTable as vt , Visitor as v " +
					" where vt.visitorId=:visitorId and vt.state=:state and v.visitorId=vt.visitorId";
			Query query = sessionFactory.getCurrentSession().createQuery(hql)
					.setParameter("visitorId", visitorId)
					.setParameter("state", state);
			return (VVisitInfoPData) query.uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
